package com.qianchang.microfinance.view.hsmywallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseFragment;
import com.qianchang.microfinance.view.base.DmBaseActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.home.HomeFragment;
import com.qianchang.microfinance.view.hsrecharge.HSWithdrawActivity;
import com.qianchang.microfinance.view.hsrecharge.HSWithdrawInputActivity;
import com.qianchang.microfinance.view.mine.RechargeActivity;
import com.qianchang.microfinance.view.mine.TradRecordActivity;
import com.qianchang.microfinance.view.mine.WithDrawActivity;

/**
 * 鸿胜科技新增我的钱包Fragment 页面
 *
 */

public class HSMyWallterFragment extends BaseFragment implements View.OnClickListener{

    DTextView tvMyMoney;
    private Button butRecharge;
    private Button withdraw;

    private FindUserBasicInfoBean findUserBasicInfoBean = new FindUserBasicInfoBean();
    private BankCardBean bankCardBean;
    private MineUserInfo mineUserInfo;



    @Override
    protected View onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_my_wallet, container, false);
        tvMyMoney = (DTextView)view.findViewById(R.id.tv_my_money);
        tvMyMoney.setOnClickListener(HSMyWallterFragment.this);
        butRecharge = (Button) view.findViewById(R.id.btn_recharge);
        withdraw = (Button) view.findViewById(R.id.btn_withdraw);
        butRecharge.setOnClickListener(this);
        withdraw.setOnClickListener(this);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mineUserInfo = (MineUserInfo) bundle.getSerializable(MineUserInfo.class.getName());
            findUserBasicInfoBean = (FindUserBasicInfoBean) bundle.getSerializable(ConstantAppInfo.BASIC_INFO_BEAN);
            if (findUserBasicInfoBean == null) findUserBasicInfoBean = new FindUserBasicInfoBean();
            //改成用接口请求
            if (mineUserInfo != null) {
                tvMyMoney.setText(FormatUtil.numKbPointFormat(mineUserInfo.getAvailable()) + "");
            }
        }
        getBankCardInfo();//获取银行卡信息

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();

        getUserInfo();

    }



    public void getUserInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(getActivity().getApplicationContext(), DMConstant.ApiURL.FIND_USER_INFO, params, new RequestCallBack<MineUserInfo>() {
            @Override
            public void onSuccess(JsonResult<MineUserInfo> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    if (null != response.getObject()) {
                        mineUserInfo = response.getObject();
                        tvMyMoney.setText(FormatUtil.numKbPointFormat(mineUserInfo.getBalance()) + "");
                    } else {
                        ToastUtil.getInstant().show(getActivity(), getString(R.string.data_error));
                    }
                } else {
                    ToastUtil.getInstant().show(getActivity(), response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getActivity(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }



    //获取绑定的银行卡信息
    public void getBankCardInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(getActivity(), DMConstant.ApiURL.FIND_CARD_DETAIL, params, new RequestCallBack<BankCardBean>() {

            @Override
            public void onSuccess(JsonResult<BankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    bankCardBean = response.getObject();
                } else {
                    ToastUtil.getInstant().show(getActivity(), response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getActivity(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.tv_right) {
            startActivity(TradRecordActivity.class);       //支付记录
            return;
        }


        ///测试使用
        if(view.getId() == R.id.btn_recharge){
            startActivity(HSRecharCentreActivity.class);
            return;
        }

        if(view.getId() == R.id.btn_withdraw){
            String myMoney = mineUserInfo.getBalance();
            Bundle bundle = new Bundle();
            bundle.putString("money",myMoney);
            startActivity(HSWithdrawInputActivity.class,bundle);

//            Bundle bundle = new Bundle();
//            bundle.putSerializable(BankCardBean.class.getName(), bankCardBean);
//            bundle.putSerializable(HomeFragment.ENTITY_KEY, findUserBasicInfoBean);
//            bundle.putSerializable(MineUserInfo.class.getName(), mineUserInfo);
//            startActivity(WithDrawActivity.class, bundle);//提现

            return;
        }


        String tips = "";
        ////是否添加银行卡绑定，0：是、1：否
        if (0 == findUserBasicInfoBean.getIsBankCardBind()) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BankCardBean.class.getName(), bankCardBean);
            bundle.putSerializable(HomeFragment.ENTITY_KEY, findUserBasicInfoBean);
            bundle.putSerializable(MineUserInfo.class.getName(), mineUserInfo);

            switch (view.getId()) {
                case R.id.btn_recharge:
                    startActivity(RechargeActivity.class, bundle);//充值
                    break;
                case R.id.btn_withdraw:
                    startActivity(WithDrawActivity.class, bundle);//提现
                    break;
            }
        } else {
            switch (view.getId()) {
                case R.id.btn_recharge:
                    tips = getString(R.string.please_bind_card_before_recharge);
                    break;
                case R.id.btn_withdraw:
                    tips = getString(R.string.please_bind_card_before_withdraw);
                    break;
            }

            ToastUtil.getInstant().show(getContext(),"正在开发敬请期待!");

            /*********
            DimengSingleButtonAlertDialog singleButtonAlertDialog = DimengSingleButtonAlertDialog.newInstance("", tips);
            singleButtonAlertDialog.setAgreeText(getString(R.string.btn_sure));
            singleButtonAlertDialog.setOnDialogClickListener(new DimengSingleButtonAlertDialog.OnDialogClickListener() {
                @Override
                public void agree() {
//                    MyWalletActivity.this.finish();
                    HSMyWallterActivity activity = (HSMyWallterActivity) getActivity();
                    activity.finish();
                }
            });
            singleButtonAlertDialog.show(getFragmentManager(), getClass().getSimpleName());
             ****************/

        }
    }


    private void refreshUi(){

    }


}
